package ytdp.careagle.com.ytdp.activity;

import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import android.text.TextUtils;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.umeng.analytics.dplus.UMADplus;

import org.greenrobot.eventbus.EventBus;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import ytdp.careagle.com.ytdp.R;
import ytdp.careagle.com.ytdp.adapter.CarBrand2Adapter;
import ytdp.careagle.com.ytdp.adapter.CarBrandAdapter;
import ytdp.careagle.com.ytdp.adapter.base.CommonAdapter;
import ytdp.careagle.com.ytdp.adapter.base.MultiItemTypeAdapter;
import ytdp.careagle.com.ytdp.adapter.base.ViewHolder;
import ytdp.careagle.com.ytdp.app.APP;
import ytdp.careagle.com.ytdp.app.manager.ImageLoadManager;
import ytdp.careagle.com.ytdp.entity.BrandRelationship;
import ytdp.careagle.com.ytdp.entity.ModelRelationship;
import ytdp.careagle.com.ytdp.entity.SeriesRelationship;
import ytdp.careagle.com.ytdp.mvp.presenter.SelectCarTypePresenter;
import ytdp.careagle.com.ytdp.mvp.view.SelectCartypeView;
import ytdp.careagle.com.ytdp.myview.MyFrameLayout;
import ytdp.careagle.com.ytdp.myview.MyLinearManager;
import ytdp.careagle.com.ytdp.myview.TitleItemDecoration;
import ytdp.careagle.com.ytdp.myview.indexbar_packet.IndexBar;
import ytdp.careagle.com.ytdp.myview.indexbar_packet.SuspensionDecoration;

public class SelectCarBrandActivity extends BaseActivity implements MyFrameLayout.ScrollListener, SelectCartypeView {

    @Bind(R.id.recyclerView)
    RecyclerView recyclerView;
    @Bind(R.id.indexBar)
    IndexBar indexBar;
    @Bind(R.id.tvSideBarHint)
    TextView tvSideBarHint;
    @Bind(R.id.iv_header_car_logo)
    ImageView ivHeaderCarLogo;
    @Bind(R.id.tv_header_car_brand)
    TextView tvHeaderCarBrand;
    @Bind(R.id.ll_child_header)
    LinearLayout llChildHeader;
    @Bind(R.id.childRecyclerView)
    RecyclerView childRecyclerView;
    @Bind(R.id.ll_layout_list2)
    RelativeLayout llLayoutList2;
    @Bind(R.id.fl_content)
    MyFrameLayout flContent;
    @Bind(R.id.fl_content3)
    FrameLayout flContent3;
    @Bind(R.id.iv_header_car_logo3)
    ImageView ivHeaderCarLogo3;
    @Bind(R.id.tv_header_car_brand3)
    TextView tvHeaderCarBrand3;
    @Bind(R.id.ll_child_header3)
    LinearLayout llChildHeader3;
    @Bind(R.id.ll_layout_list3)
    LinearLayout llLayoutList3;
    @Bind(R.id.childRecyclerView3)
    RecyclerView childRecyclerView3;
    private CarBrandAdapter mAdapter;
    private MyLinearManager mManager;
    private List<BrandRelationship.ChildrenBean> mDatas = new ArrayList<>();
    private SuspensionDecoration mDecoration;
    private boolean isFirst = true;
    private TitleItemDecoration mChildDecoration;
    private List<SeriesRelationship.ChildrenBean> mChildDatas;
    private CarBrand2Adapter mChildAdapter;
    private String imageLogo;
    private ArrayList<ModelRelationship.ChildrenBean> mChildDatas3;
    private CommonAdapter<ModelRelationship.ChildrenBean> mChildAdapter3;
    private TitleItemDecoration mChildDecoration3;
    private SelectCarTypePresenter presenter;
    private String seriesCode;
    private String imageUrl;

    @Override
    public int getLayoutId() {
        return R.layout.activity_select_car_brand;
    }

    @Override
    public void initView() {
        UMADplus.track(this, "选择车型页面", null);
        initToolbar("选择车型");
        presenter = new SelectCarTypePresenter(this);
        initRecyclerview();
        flContent.setScrollListener(this);
        initList2RecyclerView();
        llChildHeader.setClickable(true);//为了防止事件被后面的 控件获取
        initList3RecyclerView();
    }

    private void initList3RecyclerView() {
        childRecyclerView3.setLayoutManager(new MyLinearManager(this));
        mChildDatas3 = new ArrayList<>();
        mChildAdapter3 = new CommonAdapter<ModelRelationship.ChildrenBean>(this, R.layout.item_select_car_type, mChildDatas3) {
            @Override
            protected void convert(ViewHolder holder, ModelRelationship.ChildrenBean modelRelationship, int position) {
                holder.setText(R.id.tv_car_type, modelRelationship.getName());
            }
        };
        mChildDecoration3 = new TitleItemDecoration(this, mChildDatas3);
        childRecyclerView3.addItemDecoration(mChildDecoration3);
        childRecyclerView3.setAdapter(mChildAdapter3);
        mChildAdapter3.setOnItemClickListener(new MultiItemTypeAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(View view, RecyclerView.ViewHolder holder, int position) {
                ModelRelationship.ChildrenBean childrenBean = mChildDatas3.get(position);
                childrenBean.setSeriesCode(seriesCode);
                childrenBean.setImageUrl(imageUrl);
                EventBus.getDefault().post(childrenBean);
                finish();
                APP.getInstance().resetCountDown();
            }

            @Override
            public boolean onItemLongClick(View view, RecyclerView.ViewHolder holder, int position) {
                return false;
            }
        });
    }

    private void initList2RecyclerView() {
        childRecyclerView.setLayoutManager(new MyLinearManager(this));
        mChildDatas = new ArrayList<>();
        mChildAdapter = new CarBrand2Adapter(this, mChildDatas);
        childRecyclerView.setAdapter(mChildAdapter);
        childRecyclerView.addItemDecoration(mChildDecoration = new TitleItemDecoration(this, mChildDatas));
        mChildDecoration.setColorTitleBg(getResources().getColor(R.color.gray));
        mChildAdapter.setOnitemClickListener(new CarBrand2Adapter.OnitemClickListener() {

            @Override
            public void itemClick(SeriesRelationship.ChildrenBean cityBean) {
                APP.getInstance().resetCountDown();
                seriesCode = cityBean.getCode();
                imageUrl = cityBean.getImage();
                presenter.getList3Data(getWContext().get(), seriesCode);
            }
        });
    }

    private void initRecyclerview() {
        mAdapter = new CarBrandAdapter(this, mDatas);
        recyclerView.setLayoutManager(mManager = new MyLinearManager(this));
        recyclerView.setAdapter(mAdapter);
        recyclerView.addItemDecoration(mDecoration = new SuspensionDecoration(this, mDatas));
        mDecoration.setColorTitleBg(getResources().getColor(R.color.gray));
        //使用indexBar
        indexBar.setmPressedShowTextView(tvSideBarHint)//设置HintTextView
                .setNeedRealIndex(true)//设置需要真实的索引
                .setmLayoutManager(mManager);//设置RecyclerView的LayoutManage
        mAdapter.setOnitemClickListener(new CarBrandAdapter.OnitemClickListener() {
            @Override
            public void itemClick(BrandRelationship.ChildrenBean childrenBean) {
                presenter.getList2Data(getWContext().get(), childrenBean.getCode());
                setList2HeaderIcon(childrenBean);
                APP.getInstance().resetCountDown();
            }
        });
        presenter.getList1Data(getWContext().get());
    }

    private void setList2HeaderIcon(BrandRelationship.ChildrenBean childrenBean) {
        imageLogo = childrenBean.getImage();
        if (TextUtils.isEmpty(imageLogo)) {
            ivHeaderCarLogo.setImageResource(R.drawable.icon_no_brandimage);
        } else {
            ImageLoadManager.getImageLoadManager().load(imageLogo, ivHeaderCarLogo, true, R.drawable.icon_no_brandimage);
        }
        tvHeaderCarBrand.setText(childrenBean.getName());
    }

    private int dx = 0;

    @Override
    public void scroll(float dx) {
        this.dx += dx;
    }

    @Override
    public void up() {
        if (dx > 50) {
            //TODO 向右边滑动
            if (llLayoutList3.getTranslationX() == 0) {
                closeList3();
            } else if (llLayoutList3.getTranslationX() == llLayoutList3.getWidth() &&
                    llLayoutList2.getTranslationX() == 0) {
                closeList2();
            }
        } else if (dx < -50) {
            //TODO 向左边滑动
            if (llLayoutList3.getTranslationX() == llLayoutList3.getWidth() &&
                    llLayoutList2.getTranslationX() == llLayoutList2.getWidth()) {
                openlist2();
            } else if (llLayoutList2.getTranslationX() == 0 &&
                    llLayoutList3.getTranslationX() == llLayoutList3.getWidth()) {
                openlist3();
            }
        }
        dx = 0;
    }

    private void openlist2() {
        if (mChildDatas.size() != 0) {
            llLayoutList2.animate().translationX(0).setDuration(200).start();
        }
    }

    private void closeList3() {
        llLayoutList3.animate().translationX(llLayoutList3.getWidth()).setDuration(200).start();
    }

    private void closeList2() {
        llLayoutList2.animate().translationX(llLayoutList2.getWidth()).setDuration(200).start();
    }

    private void openlist3() {
        if (mChildDatas3.size() != 0) {
            llLayoutList3.animate().translationX(0).setDuration(200).start();
        }
    }

    @Override
    public void onWindowFocusChanged(boolean hasFocus) {
        super.onWindowFocusChanged(hasFocus);
        if (hasFocus && isFirst) {
            isFirst = false;
            llLayoutList2.setTranslationX(llLayoutList2.getWidth());
            llLayoutList3.setTranslationX(llLayoutList3.getWidth());
        }
    }

    @Override
    public void setList1Data(List<BrandRelationship> dataBeen) {
        mDatas.clear();
        for (BrandRelationship dataBean : dataBeen) {
            List<BrandRelationship.ChildrenBean> children = dataBean.getChildren();
            mDatas.addAll(children);
        }
        indexBar.setmSourceDatas(mDatas)//设置数据
                .invalidate();
        mAdapter.setDatas(mDatas);
        mDecoration.setmDatas(mDatas);
        mAdapter.notifyDataSetChanged();
    }

    @Override
    public void setList2Data(List<SeriesRelationship> data) {
        mChildDatas.clear();
        for (SeriesRelationship dataBean : data) {
            List<SeriesRelationship.ChildrenBean> children = dataBean.getChildren();
            mChildDatas.addAll(children);
        }
        mChildAdapter.setDatas(mChildDatas);
        mChildDecoration.setmDatas(mChildDatas);
        mChildAdapter.notifyDataSetChanged();
        openlist2();
        closeList3();
        mChildDatas3.clear();
    }

    @Override
    public void setList3Data(List<ModelRelationship> data) {
        mChildDatas3.clear();
        for (ModelRelationship dataBean : data) {
            mChildDatas3.addAll(dataBean.getChildren());
        }
        mChildAdapter3.notifyDataSetChanged();
        openlist3();
    }

}
